﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;
using System.Diagnostics;
using System.Threading;
using common;
using HTF_HMI;
using HTF_HMI.HTF_HMI;
using log4net;
using System.Reflection;
using System.Data.SqlClient;

namespace HMI_MAIN
{
    public partial class HTFMain : Form
    {
        public static readonly string ops = ConfigurationManager.AppSettings["Location"];
        public static string connStr = ConfigReader.getDBString();
        //OleDbConnection connnection = new OleDbConnection(connStr);

        private bool Is_Timer_First;

        private bool Is_Update_Files = true;
        AutoSizeFormClass asc = new AutoSizeFormClass();

        private string Func_Caption;
        private Form Form_Func;
        public string User_Name;
        public string User_Password;
        public string UserType;
        //private FormAbout Form_About;

       // FormSplash frmSplash;     

        public HTFMain()
        {
            InitializeComponent();

            DBClass.DBHelper a = new DBClass.DBHelper(connStr);

            //   this.frmSplash = frmSplash;////splash   
            //Form_About = new FormAbout();
            //Form_About.Hide();

            SetNodeImg();

            Form_Func = null;
            Func_Caption = "";
        }

        private void FormMain_Load(object sender, EventArgs e)//
        {
            Is_Timer_First = true;
            timer_Tick(null, null);
            if (ops == "Entry")
            {
                DeleteTreeNode(treeView_Menu, 1);
            }
            if (ops == "Exit")
            {
                DeleteTreeNode(treeView_Menu, 0);
            }
            treeView_Menu.ExpandAll();
        }

        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Close_All();
        }


        private void timer_Tick(object sender, EventArgs e)//
        {
            timer.Enabled = false;
            int i = int.Parse((string)timer.Tag);

            if ((i % 20) == 0)
            {
                if (Is_Timer_First)
                {
                    Check_DB_Connection(true);
                    Is_Timer_First = false;
                }
                else
                {                    
                    Check_DB_Connection(false);
                }
            }           

            if ((i % 5) == 0)
            {
                if (label_Caption.Text == Func_Caption)
                {
                }
                else
                {
                    if (label_Caption.ForeColor == Color.Black)
                    {
                        label_Caption.ForeColor = Color.Red;
                    }
                    else
                    {
                        label_Caption.ForeColor = Color.Black;
                    }
                }
            }


            if ((i % 10) == 9)
            {
                if (Is_Update_Files)
                {
                    Is_Update_Files = false;
                }
            }
            if (i == 19)
            {
                i = 0;
            }
            else
            {
                i++;
            }
            timer.Tag = i.ToString();
            timer.Enabled = true;
        }

        public void Check_DB_Connection(bool Is_First)//
        { 
            try
            {
                //if (connnection.State == ConnectionState.Closed)
                //{
                //    connnection.Open();
                //}

                //using (IDbConnection connection = new SqlConnection(connStr))
                //{
                   
                //}


                if (Is_First)
                {                  
                 //   button_Logout_Click(null, null);;
                    this.Left = 0;
                    this.Top = 0;
                    this.Width = Screen.PrimaryScreen.WorkingArea.Width;//1024;
                    this.Height = Screen.PrimaryScreen.WorkingArea.Width;
                    asc.controlAutoSize(this);
                    this.MinimizeBox = true;
                    
                }
                lblconnectcheck.Visible = false;
            }
            catch (Exception ex)
            {
                this.Left = 0;
                this.Top = 0;
                this.Width = Screen.PrimaryScreen.WorkingArea.Width;//1024;
                this.Height = Screen.PrimaryScreen.WorkingArea.Width;
                asc.controlAutoSize(this);
                this.MinimizeBox = true;
                lblconnectcheck.Visible = true;

                //connnection.Close();
                DBClass.Log.InsertLog(ex.Message);
            }

        }

        private void Show_DB_Connect_Error()
        {
            this.Hide();

            treeView_Menu.SelectedNode = null;
            treeView_Menu.CollapseAll();

            Func_Caption = "";
            if (Form_Func == null)
            {
            }
            else
            {
                Form_Func.Dispose();
                Form_Func = null;
            }

            panel_Func_Form.Show();
            label_Caption.Text = "请点击左侧的菜单，进入画面";
            label_Caption.Left = (panel_Caption.Width - label_Caption.Width) / 2;
            label_Caption.ForeColor = Color.Black;

            panel_Login.Hide();

            this.MinimizeBox = false;
            this.MaximizeBox = false;            
            panel_DB_Connect_Error.Show();

            this.Width = 196;
            this.Height = 90;
            this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
            this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;

            this.Show();

            //Form_About.Hide();
        }

        private void button_Logout_Click(object sender, EventArgs e)
        {
            
            this.Hide();

            treeView_Menu.SelectedNode = null;
            treeView_Menu.CollapseAll();

            Func_Caption = "";
            if (Form_Func == null)
            {
            }
            else
            {
                Form_Func.Dispose();
                Form_Func = null;
            }

            panel_Func_Form.Show();
            label_Caption.Text = "请点击左侧的菜单，进入画面";
            label_Caption.Left = (panel_Caption.Width - label_Caption.Width) / 2;
            label_Caption.ForeColor = Color.Black;
            cmbbc.SelectedIndex =0;

            this.MinimizeBox = false;
            this.MaximizeBox = false;
             panel_Login.Show();
           
            //Get_User_Name();
            panel_DB_Connect_Error.Hide();

            this.Width = 255;
            this.Height =255;
            this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
            this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;

            this.Show();
            textBox_Password.Text = "";
            textBox_Password.Focus();

            //Form_About.Hide();
        }

        public void User_LogOut()
        {
           
            this.Hide();

            treeView_Menu.SelectedNode = null;
            treeView_Menu.CollapseAll();

            Func_Caption = "";
            if (Form_Func == null)
            {
            }
            else
            {
                Form_Func.Dispose();
                Form_Func = null;
            }

            panel_Func_Form.Show();
            label_Caption.Text = "请点击左侧的菜单，进入画面";
            label_Caption.Left = (panel_Caption.Width - label_Caption.Width) / 2;
            label_Caption.ForeColor = Color.Black;
            cmbbc.SelectedIndex = 0;

            this.MinimizeBox = false;
            this.MaximizeBox = false;
            panel_Login.Show();
        
            panel_DB_Connect_Error.Hide();

            this.Width = 200;
            this.Height = 180;
            this.Left = (Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2;
            this.Top = (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2;

            this.Show();
            textBox_Password.Text = "";
            textBox_Password.Focus();

        }

        private void Get_User_Name()
        {
            //comboBox_User_Name.Items.Clear();
            //OleDbCommand command = new OleDbCommand();
            //command.CommandType = System.Data.CommandType.Text;
            //command.Connection = connnection;

            //if (connnection.State == ConnectionState.Closed)connnection.Open();
            //command.CommandText = "SELECT USERNAME FROM MANAGE_USER ";
            //OleDbDataReader reader = command.ExecuteReader();
            ////OracleDataReader reader = cmdcommand.ExecuteReader(CommandBehavior.CloseConnection);

            //while (reader.Read())
            //{
            //    comboBox_User_Name.Items.Add(reader[0]);
            //}            
            //comboBox_User_Name.Items.Add("");
            //reader.Close();
            //if (comboBox_User_Name.Items.Count > 0)
            //{
            //    comboBox_User_Name.SelectedIndex = 0;
            //}
            //else
            //{
            //    comboBox_User_Name.Text = "";
            //}
        }

        private void button_Login_Click(object sender, EventArgs e)
        {
            ////OleDbCommand command = new OleDbCommand();
            ////command.Connection = connnection;

            //OleDbCommand command = new OleDbCommand();
            //command.CommandType = System.Data.CommandType.Text;
            //command.Connection = connnection;

            //if (connnection.State == ConnectionState.Closed) connnection.Open();
            //string sqlstr;
            //loginname.Visible = false;
            //loginpassword.Visible = false;
            //if (textBox_Password.Text == "")
            //{
            //    loginpassword.Visible = true;
            //    textBox_Password.Text = "";
            //    textBox_Password.Focus();
            //    return;
            //}
            //if (comboBox_User_Name.Text == "" && textBox_Password.Text == "*123")
            //{
            //    this.Hide();

            //    panel_Login.Hide();

            //    this.Left = 0;
            //    this.Top = 0;
            //    this.Width = 1024;
            //    this.Height = 738;

            //    this.MinimizeBox = true;           
            //    this.Show();
            //}
            //else
            //{
            //    if (comboBox_User_Name.Text == "")
            //    {
            //        loginname.Visible = true;
            //        comboBox_User_Name.Focus();
            //        return;
            //    }
            //    command.CommandText = "SELECT UserName, PASSWORD,USERTYPE FROM MANAGE_USER"
            //                                     + " WHERE USERNAME='" + comboBox_User_Name.SelectedItem.ToString().Trim()
            //                                     + "' AND PASSWORD='" + textBox_Password.Text.Trim()+"'";

            //    OleDbDataReader reader = command.ExecuteReader();

            //    if (reader.Read())
            //    {
            //        User_Name = reader[0].ToString();
            //        User_Password = reader[1].ToString();

            //        this.Hide();
            //        panel_Login.Hide();

            //        this.Left = 0;
            //        this.Top = 0;
            //        this.Width = Screen.PrimaryScreen.WorkingArea.Width;
            //        this.Height = Screen.PrimaryScreen.WorkingArea.Width;
            //        asc.controlAutoSize(this);
            //        this.MinimizeBox = true;                    
              
            //        this.Show();
            //    }
            //    else
            //    {                    
            //        loginpassword.Visible = true;
            //        textBox_Password.Text = "";
            //        textBox_Password.Focus();
            //    }
            //    reader.Close();
            //}
        }


        private void treeView_Menu_AfterSelect(object sender, TreeViewEventArgs e)
        {
           
            TreeNode Node = treeView_Menu.SelectedNode;
            if (Node.FirstNode != null)
            {
                return;
            }

            if (Node.Text == Func_Caption)
            {
                return;
            }

            Func_Caption = Node.Text;

            if (Form_Func == null)
            {
                panel_Func_Form.Hide();
            }
            else
            {
                Form_Func.Close();
                Form_Func.Dispose();
                Form_Func = null;
            }
            
            switch (Func_Caption)
            {
                case "修改钢坯信息": 
                    //Form_Func = new EntrySheetSelect();
                    break;               

                //case "炉内映像":
                //    Form_Func = new Act_BatchReport();
                //    break;

                case "设备类别":
                    Form_Func = new AUDIT_DEVICE();
                    break;
                case "设备清单":
                    Form_Func = new AUDIT_TASK();
                    break;

                case "巡检任务查询":
                    Form_Func = new AUDIT_TASK_QUERY();
                    break;

                case "巡检点位设定":
                    Form_Func = new AUDIT_DEVICE();
                    break;

                case "巡检路线设定":
                    Form_Func = new AUDIT_TASK();
                    break;
                case "巡检任务周期设定":
                    Form_Func = new AUDIT_PERIOD();
                    break;

                case "排烟":
                    string[,] tags3 = {
                        { "换热器后助燃空气温度", "rhrqhzrkqwd" },
                        { "换热器前烟气温度", "rhrqqyqwd" },
                        { "换热器后烟气温度", "rhrqhyqwd" }
                    };
                    //Form_Func = new MeasManagerCtrl(Func_Caption, tags3);
                    break;                
                case "水冷":
                    string[,] tags5 = {
                        { "冷却水进水压力", "rlqsjsyl" },
                        { "冷却水进水流量", "rlqsjsll" },
                        { "冷却水进水温度", "rlqsjswd" }
                    };
                    //Form_Func = new MeasManagerCtrl(Func_Caption, tags5);
                    break;
                case "热值分析仪":
                    string[,] tags6 = {
                        { "热值分析", "rrzfx" }
                    };
                    //Form_Func = new MeasManagerCtrl(Func_Caption, tags6);
                    break;                


                default:
                    //Form_Func = new EntrySheetSelect();
                    return;                    
            }


            //Form_Func.MdiParent = this.splitContainer1.Panel2;
            //this.splitContainer1.Panel2.s

            //Form_Func.MdiParent = this;

            //Form_Func.Left = panel_Func_Form.Left - 2;
            //Form_Func.Top = panel_Func_Form.Top - 2;
           

            Form_Func.MdiParent = this;
            Form_Func.Parent = splitContainer1.Panel2;
            Form_Func.Width = panel_Func_Form.Width;
            Form_Func.Height = panel_Func_Form.Height-50;
            Form_Func.Show();



            label_Caption.Text = Func_Caption;

            label_Caption.Left = (panel_Caption.Width - label_Caption.Width) / 2;
            label_Caption.ForeColor = Color.Black;
        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            Close_All();            
        }

        private void button_Exit_Login_Click(object sender, EventArgs e)
        {
            Close_All();
        }

        private void Close_All()
        {
            //if (Form_Func == null)
            //{
            //}
            //else
            //{
            //    Form_Func.Close();
            //    Form_Func.Dispose();
            //}

            //if (connnection == null)
            //{
            //}
            //else
            //{
            //    connnection.Close(); 
            //    connnection.Dispose();
            //}
            
        }

        private void cmbbc_SelectedIndexChanged(object sender, EventArgs e)
        {
            Get_User_Name();
            textBox_Password.Text = "";
            textBox_Password.Focus();
        }
        private void comboBox_User_Name_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBox_Password.Focus();
        }

        private void textBox_Password_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                button_Login_Click(sender,e);
            }
        }

        protected void DeleteTreeNode(TreeView tree,int nodeno)
        {
            DeleteSubNode(tree.Nodes[nodeno]);
            tree.Nodes[nodeno].Remove();
        }
       
        protected void DeleteSubNode(TreeNode parent)
        {
            if (parent == null)
            {
                return;
            }           
            if (parent.Nodes.Count == 0)
            {
            }
            else
            {
                for (int i = parent.Nodes.Count - 1; i >= 0; i--)
                {
                    parent.Nodes.RemoveAt(i);
                }
            }
        }

        private void panel_Menu_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}